package com.pearadmin.pro.modules.sys.rest;

import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.utils.redis.RedisUtil;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.sys.domain.SysRetrievePwdEntity;
import com.pearadmin.pro.modules.sys.service.SysRetrievePwdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = {"找回密码"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "retrievePwd")
public class SysRetrievePwdController extends BaseController {
    @Resource
    private SysRetrievePwdService sysRetrievePwdService;


    @Resource // redis工具类
    private RedisUtil redisUtil;


    @PostMapping("resetPassword")
    @ApiOperation(value = "密码重置")
    public Result resetPassword(@RequestBody SysRetrievePwdEntity sysRetrievePwd) {
        // 参数校验
        Result result = parameterCalibration(sysRetrievePwd);
        if (result.getCode() == 500) {
            return result;
        }
        return sysRetrievePwdService.resetPassword(sysRetrievePwd);
    }



















    /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------*/

    /**
     * 参数校验
     *
     * @author ZhouXingYun
     * @date 2022/4/14 15:01
     */
    public Result parameterCalibration(SysRetrievePwdEntity sysRetrievePwd) {
        //判断是否传过来空值
        if (sysRetrievePwd == null) {
            return failure("空参");
        }
        //判断两次密码是否相同
        if (!sysRetrievePwd.getNewPassword().equals(sysRetrievePwd.getConfirmPassword())) {
            return failure("两次密码不同，请确认之后再输入！！！");
        }
        // 获取redis的授权码，与用户输入对比
        String authorizationCode = (String) redisUtil.get(sysRetrievePwd.getEmail());
        if (StringUtils.isEmpty(authorizationCode) || !authorizationCode.equals(sysRetrievePwd.getAuthCode())) {
            return failure("授权码无效，请检查后输入！！！");
        }
        return success();
    }


}
